package com.zzh.zzhaiagent.controller;


import com.zzh.zzhaiagent.domain.dto.UserEmailAuthDTO;
import com.zzh.zzhaiagent.domain.dto.UserProfilesLoginDTO;
import com.zzh.zzhaiagent.domain.dto.UserProfilesUpdateDTO;
import com.zzh.zzhaiagent.domain.vo.UserLoginTokenVO;
import com.zzh.zzhaiagent.domain.vo.UserProfilesVO;
import com.zzh.zzhaiagent.protection.rate.RateLimiter;
import com.zzh.zzhaiagent.protection.rate.resolver.ClientIpRateLimiterKeyResolver;
import com.zzh.zzhaiagent.service.IUserProfilesService;
import com.zzh.zzhaiagent.utils.ResultUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

/**
 * @author zzh
 * @since 2025-05-07
 */
@RestController
@RequestMapping("/userProfiles")
public class UserProfilesController {

    @Resource
    private IUserProfilesService userProfilesService;

    //实现用户的登录功能 email登录
    //针对这些接口进行限流控制
    @PostMapping("/login/email")
    @RateLimiter(keyResolver = ClientIpRateLimiterKeyResolver.class,count = 2,time = 60)//针对IP进行限流，一分钟内访问5次
    public ResultUtils<UserLoginTokenVO> loginEmail(@RequestBody UserProfilesLoginDTO userProfilesLoginDTO) {
        return ResultUtils.okLogin(userProfilesService.loginEmail(userProfilesLoginDTO));
    }

    //todo 当第一次邮箱验证的时候会让其进行账户密码的输入！(默认提供账户邮箱+前缀)


    //实现用户的登录功能 phone登录
    @PostMapping("/login/phone")
    @RateLimiter(keyResolver = ClientIpRateLimiterKeyResolver.class,count = 2,time = 60)//针对IP进行限流，一分钟内访问5次
    public ResultUtils<UserLoginTokenVO> loginPhone(@RequestBody UserProfilesLoginDTO userProfilesLoginDTO) {
        //TODO 实现用户登录功能
        return ResultUtils.okLogin(userProfilesService.loginPhone(userProfilesLoginDTO));
    }

    //实现用户账户密码登录
    @PostMapping("/login/password")
    @RateLimiter(keyResolver = ClientIpRateLimiterKeyResolver.class,count = 2,time = 60)//针对IP进行限流，一分钟内访问5次
    public ResultUtils<UserLoginTokenVO> loginPassword(@RequestBody UserProfilesLoginDTO userProfilesLoginDTO) {
        //TODO 实现用户登录功能
        return ResultUtils.okLogin(userProfilesService.loginPassword(userProfilesLoginDTO));
    }

    //实现邮箱认证功能
    @PostMapping("/email/auth")
    public ResultUtils emailAuth(@RequestBody UserEmailAuthDTO userEmailAuthDTO) {
        return userProfilesService.emailAuth(userEmailAuthDTO);
    }


    @GetMapping("/info")
    public ResultUtils<UserProfilesVO> getUserInfo(HttpServletRequest request) {
        return ResultUtils.ok(userProfilesService.getUserInfo(request));
    }

    /**
     * 刷新token
     * @param uuid refreshToken的uuid 标识
     * @return 新的token
     */
    @GetMapping("/refreshToken")
    public ResultUtils<UserLoginTokenVO> refreshToken(@RequestParam String uuid) {
        return ResultUtils.okRefresh(userProfilesService.refreshToken(uuid));
    }


    //第一次登录成功后，进行账户密码的修改 （AccessToken携带的用户信息）
    @PutMapping("/updatePassword")
    public ResultUtils<String> updatePassword(String password, HttpServletRequest request) {//通过账户来修改密码
        return ResultUtils.ok(userProfilesService.updatePassword(password,request));
    }

    //修改个人用户信息
    @PutMapping("/updateInfo")
    public ResultUtils<String> updateInfo(@RequestBody UserProfilesUpdateDTO userProfilesVO, HttpServletRequest request) {
        return ResultUtils.ok(userProfilesService.updateInfo(userProfilesVO,request));
    }


}

